﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
using Microsoft.SharePoint.Administration;

namespace zevenseas.ThemeConfig.ApplicationPages
{
    public class ApplyTheme : WebAdminPageBase
    {
        // Fields
        protected HtmlInputHidden currentTheme;
        protected SPThemes dsSPThemes;
        protected DataList lstTemplates;
        private const int numThemes = 0x40;
        public string[] sa;
        public string[] sb;
        public string[] sc;
        public string[] sd;
        public int selectedThemeIndex;
        public int themeIndex;
        protected HtmlInputHidden themes;

        // Methods
        protected override void OnLoad(EventArgs e)
        {               
            DataView view = FilterThemes();
            view.Sort = "DisplayName";

            this.lstTemplates.DataSource = view;
            this.lstTemplates.DataBind();

            this.currentTheme.Value = "images/thnone.gif";
            string theme = base.Web.Theme;
            if (string.IsNullOrEmpty(theme))
            {
                theme = "none";
            }

            this.sa = new string[0x40];
            this.sb = new string[0x40];
            this.sc = new string[0x40];
            this.sd = new string[0x40];

            IEnumerator enumerator = this.lstTemplates.Items.GetEnumerator();
            while (enumerator.MoveNext())
            {
                DataListItem current = (DataListItem)enumerator.Current;

                this.sb[this.themeIndex] = ((HtmlInputControl)current.FindControl("ThemeN")).Value;
                this.sa[this.themeIndex] = ((HtmlInputControl)current.FindControl("Preview")).Value;
                this.sc[this.themeIndex] = ((HtmlInputControl)current.FindControl("ThemeId")).Value;
                this.sd[this.themeIndex] = ((HtmlInputControl)current.FindControl("Description")).Value;

                if (!Page.IsPostBack)
                {
                    if (this.sc[this.themeIndex] == theme)
                    {
                        this.currentTheme.Value = this.sa[this.themeIndex];
                        this.selectedThemeIndex = this.themeIndex;
                        this.themes.Value = Convert.ToString(this.themeIndex);
                    }
                }
                this.themeIndex++;
            }

            base.OnLoad(e);
        }

        private List<string> BlockedThemes(ref List<string> blockedThemes)
        {
            SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
            Themes settings = webApplication.GetChild<Themes>("ThemeCollection");
            if (settings != null)
            {
                if (settings.TemplateIDs != null)
                {
                    blockedThemes = settings.TemplateIDs;
                }
            }

            return blockedThemes;
        }

        private DataView FilterThemes()
        {
            DataTable allThemesTable = SPContext.Current.Site.WebApplication.GetAllThemes().ToTable();

            List<string> blockedThemes = new List<string>();
            BlockedThemes(ref blockedThemes);

            DataTable themesTable = new DataTable();
            DataTable blockedThemesTable = new DataTable();

            themesTable = allThemesTable.Clone();
            blockedThemesTable = allThemesTable.Clone();

            foreach (DataRow row in allThemesTable.Rows)
            {
                bool isBlocked = false;
                foreach (string templateId in blockedThemes)
                {
                    if (row["TemplateID"].ToString() == templateId)
                    {
                        isBlocked = true;
                        break;
                    }
                }

                if (!isBlocked)
                {
                    DataRow themeRow = themesTable.NewRow();
                    themeRow.ItemArray = row.ItemArray;
                    themesTable.Rows.Add(themeRow);
                }                
            }

            return new DataView(themesTable);            
        }

        public void Submit1_Click(object sender, EventArgs e)
        {
            int index = Convert.ToInt32(this.themes.Value);
            if (index >= 0)
            {
                base.Web.ApplyTheme(this.sc[index]);
                SPUtility.Redirect("settings.aspx", SPRedirectFlags.RelativeToLayoutsPage, this.Context);
            }
        }

        // Properties
        protected override SPBasePermissions RightsRequired
        {
            get
            {
                return (SPBasePermissions.EmptyMask | SPBasePermissions.ApplyThemeAndBorder);
            }
        }
    }
}

 

 



